package com.ljtxxhwj.myblob.controller;

import com.ljtxxhwj.myblob.entity.Chat;
import com.ljtxxhwj.myblob.entity.ChatRecord;
import com.ljtxxhwj.myblob.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    ChatService chatService;

    @PostMapping("/add.do")
    public String addChat(@RequestBody Chat chat) {
//        System.out.println("chat = " + chat);
        chatService.addChat(chat);
        return "{ addChat : success }";
    }

    @GetMapping("/get.do")
    public Chat getChat(@RequestParam("chatId") int chatId){
//        System.out.println("chatId = " + chatId);
        return chatService.getChatById(chatId);
    }

    @GetMapping("/getMy.do")
    public List<Chat>getMyChat(@RequestParam("userId") int userId){
//        System.out.println("userId = " + userId);
        return chatService.getMyChat(userId);
    }

    @PostMapping("/addChatRecord.do")
    public String addChatRecord(@RequestBody ChatRecord chatRecord){

        Timestamp chatRecordDate=new Timestamp(new java.util.Date().getTime());
        System.out.println("chatRecordDate = " + chatRecordDate);
        chatRecord.setChatRecordDate(chatRecordDate);
        chatService.addChatRecord(chatRecord);
        return "{ addChatRecord : success }";
    }

    @GetMapping("/getChatRecord.do")
    public List<ChatRecord>getChatRecord(@RequestParam("chatId") int chatId){
        return chatService.getChatRecord(chatId);
    }
}
